<?php

/*
 * Copyright (C) 2014 abc
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Description of Container
 *
 * @author vlasta81
 */

namespace system;

use PDO;
//use PDOException;

final class database {

    private $host = 'localhost';
    private $user = 'root';
    private $pass = 'root';
    private $dbname = 'phpnotes';
    private $dbh;
    private $stmt;

    public function __construct($type) {
        $dsn = $type . ':host=' . $this->host . ';dbname=' . $this->dbname;
        $options = array(PDO::ATTR_PERSISTENT => TRUE, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
        $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        $charsetDatabase = 'SET NAMES ' . CHARSET;
        $this->dbh->exec($charsetDatabase);
    }

    public function query($query) {
        $this->stmt = $this->dbh->prepare($query);
    }

    public function bind($param, $value, $type = NULL) {
        if (is_null($type)) :
            switch (TRUE) :
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            endswitch;
        endif;
        $this->stmt->bindValue($param, $value, $type);
    }

    public function execute() {
        return $this->stmt->execute();
    }

    public function resultset() {
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function single() {
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function rowCount() {
        return $this->stmt->rowCount();
    }

    public function lastInsertId() {
        return $this->dbh->lastInsertId();
    }

    public function beginTransaction() {
        return $this->dbh->beginTransaction();
    }

    public function endTransaction() {
        return $this->dbh->commit();
    }

    public function cancelTransaction() {
        return $this->dbh->rollBack();
    }

    public function debugDumpParams() {
        return $this->stmt->debugDumpParams();
    }

}

//EXAMPLES
//$database = new Database();
//$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');
//$database->bind(':fname', 'John');
//$database->bind(':lname', 'Smith');
//$database->bind(':age', '24');
//$database->bind(':gender', 'male');
//$database->execute();
//echo $database->lastInsertId();
//
//$database->beginTransaction();
//$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');
//$database->bind(':fname', 'Jenny');
//$database->bind(':lname', 'Smith');
//$database->bind(':age', '23');
//$database->bind(':gender', 'female');
//$database->execute();
//$database->bind(':fname', 'Jilly');
//$database->bind(':lname', 'Smith');
//$database->bind(':age', '25');
//$database->bind(':gender', 'female');
//$database->execute();
//echo $database->lastInsertId();
//$database->endTransaction();
//
//$database->query('SELECT FName, LName, Age, Gender FROM mytable WHERE FName = :fname');
//$database->bind(':fname', 'Jenny');
//$row = $database->single();
//echo "<pre>";
//print_r($row);
//echo "</pre>";
//
//$database->query('SELECT FName, LName, Age, Gender FROM mytable WHERE LName = :lname');
//$database->bind(':lname', 'Smith');
//$rows = $database->resultset();
//echo "<pre>";
//print_r($rows);
//echo "</pre>";
//echo $database->rowCount();

